/* Initialization code for SDL */
#include <SDL/SDL.h>

extern int  SDL_VideoInit(Uint32 flags, void *user_data);
extern void SDL_VideoQuit(void);

/* The initialized subsystems */
Uint32 initialized = 0;

#ifdef CHECK_LEAKS
int surfaces_allocated = 0;
#endif

int SDL_Init(Uint32 flags, void *user_data)
{
	initialized |= SDL_INIT_VIDEO;

	/* Initialize the video/event subsystem */
	if ( SDL_VideoInit(flags, user_data) < 0 ) {
			return(-1);
	}

	/* Everything is initialized */
	if ( !(flags & SDL_INIT_NOPARACHUTE) ) {
		SDL_InstallParachute();
	}
	return(0);
}

void SDL_Quit(void)
{
	if ( initialized & SDL_INIT_VIDEO ) {
		SDL_VideoQuit();
		initialized &= ~SDL_INIT_VIDEO;
	}

	SDL_ClearError();

#ifdef CHECK_LEAKS
	if ( surfaces_allocated != 0 ) {
		fprintf(stderr, "SDL Warning: %d SDL surfaces extant\n", 
							surfaces_allocated);
	}
#endif
}

static Uint16 SDL_Swap16(Uint16 D) {
	return((D<<8)|(D>>8));
}

static Uint32 SDL_Swap32(Uint32 D) {
	return((D<<24)|((D<<8)&0x00FF0000)|((D>>8)&0x0000FF00)|(D>>24));
}

Uint16 SDL_ReadLE16 (SDL_RWops *src)
{
	Uint16 value;

	SDL_RWread(src, &value, (sizeof value), 1);
	return(SDL_SwapLE16(value));
}
Uint16 SDL_ReadBE16 (SDL_RWops *src)
{
	Uint16 value;

	SDL_RWread(src, &value, (sizeof value), 1);
	return(SDL_SwapBE16(value));
}
Uint32 SDL_ReadLE32 (SDL_RWops *src)
{
	Uint32 value;

	SDL_RWread(src, &value, (sizeof value), 1);
	return(SDL_SwapLE32(value));
}
Uint32 SDL_ReadBE32 (SDL_RWops *src)
{
	Uint32 value;

	SDL_RWread(src, &value, (sizeof value), 1);
	return(SDL_SwapBE32(value));
}

int SDL_WriteLE16 (SDL_RWops *dst, Uint16 value)
{
	value = SDL_SwapLE16(value);
	return(SDL_RWwrite(dst, &value, (sizeof value), 1));
}
int SDL_WriteBE16 (SDL_RWops *dst, Uint16 value)
{
	value = SDL_SwapBE16(value);
	return(SDL_RWwrite(dst, &value, (sizeof value), 1));
}
int SDL_WriteLE32 (SDL_RWops *dst, Uint32 value)
{
	value = SDL_SwapLE32(value);
	return(SDL_RWwrite(dst, &value, (sizeof value), 1));
}
int SDL_WriteBE32 (SDL_RWops *dst, Uint32 value)
{
	value = SDL_SwapBE32(value);
	return(SDL_RWwrite(dst, &value, (sizeof value), 1));
}
